<template>
  <div class="production-test-case-page">
    <el-card
      class="aui-card--fill"
      shadow="never">
      <!--         @keyup.enter.native="getDataList()"-->
      <el-form
        :inline="true"
        :model="dataForm">
        <!--        <el-form-item>-->

        <!--          <product-type-select :type="dataForm.productTypeId" @change="onTypeChanged"></product-type-select>-->
        <!--&lt;!&ndash;          <el-select&ndash;&gt;-->
        <!--&lt;!&ndash;            clearable&ndash;&gt;-->
        <!--&lt;!&ndash;            @change="onTypeChanged"&ndash;&gt;-->
        <!--&lt;!&ndash;            placeholder="所属品类"&ndash;&gt;-->
        <!--&lt;!&ndash;            v-model="dataForm.productTypeId"&ndash;&gt;-->
        <!--&lt;!&ndash;          >&ndash;&gt;-->
        <!--&lt;!&ndash;            <el-option&ndash;&gt;-->
        <!--&lt;!&ndash;              v-for="type in producttypes"&ndash;&gt;-->
        <!--&lt;!&ndash;              :key="type.id"&ndash;&gt;-->
        <!--&lt;!&ndash;              :value="type.id"&ndash;&gt;-->
        <!--&lt;!&ndash;              >{{ type.productType }}</el-option&ndash;&gt;-->
        <!--&lt;!&ndash;            >&ndash;&gt;-->
        <!--&lt;!&ndash;          </el-select>&ndash;&gt;-->
        <!--        </el-form-item>-->

        <!--        <el-form-item>-->
        <!--          <el-input-->
        <!--            clearable-->
        <!--            placeholder="用例名称"-->
        <!--            v-model="dataForm.taskName"-->
        <!--          >-->
        <!--            <el-button slot="append" @click="getDataList()">-->
        <!--              <i class="el-icon-search"></i>-->
        <!--            </el-button>-->
        <!--          </el-input>-->
        <!--        </el-form-item>-->
        <el-form-item>
          <!--            -->
          <el-button
            type="primary"
            @click="clickOnAddOrUpdate()">
            新建用例
          </el-button>
        </el-form-item>
      </el-form>

      <div class="table-container">
        <!--                                        :data="user-data"
        v-loading="loading"
                        -->
        <el-table
          border
          style="width: 100%"
          :data="dataList">
          <!--                    :label="$t('appData.user.table.title')"-->
          <el-table-column
            align="center"
            header-align="center"
            label="用例名称"
            prop="taskName"
            width="200"
          />

          <el-table-column
            align="center"
            header-align="center"
            label="所属品类"
            prop="productTypeName"
            width="200"
          >

            <template slot-scope="scope">

              {{ getProductTypeName(scope.row) }}
              <!--              {{scope.row.productTypeName}}({{scope.row.productTypeCode}})-->
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('news.content')"
            header-align="center"
            prop="taskContent"
          >
            <template slot-scope="scope">
              <div
                class="task-content"
                v-html="scope.row.taskContent"/>

            </template>
          </el-table-column>

          <el-table-column
            :label="$t('handle')"
            align="center"
            fixed="right"
            header-align="center"
            width="150"
          >
            <template slot-scope="scope">
              <!--                                    v-if="$hasPermission('monitor:mmonitorapilist:update')"
-->
              <el-button
                @click="clickOnAddOrUpdate(scope.row.id)"
                size="small"
                type="primary"
              >{{ $t("update") }}
              </el-button>
              <!--              v-if="$hasPermission('monitor:mmonitorapilist:delete')"-->
              <el-button
                @click="deleteHandle(scope.row.id)"
                size="small"
                type="danger"
              >{{ $t("delete") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="page"
          :page-size="limit"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          @current-change="pageCurrentChangeHandle"
          @size-change="pageSizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
        />
        <test-case-add-or-update
          @refreshDataList="getDataList"
          ref="addOrUpdate"
          v-if="addOrUpdateVisible"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import TestCaseAddOrUpdate from './testCase-add-or-update'
import ProductTypeSelect from '../../../components/product.type.select'

export default {
  name: 'ProductionTestCasePage',
  mixins: [mixinViewModule],
  components: { ProductTypeSelect, TestCaseAddOrUpdate },
  data () {
    return {
      producttypes: [],
      mixinViewModuleOptions: {
        getDataListURL: '/product/ptestcase/page',
        getDataListIsPage: true,
        deleteURL: '/product/ptestcase',
        deleteIsBatch: true
        // exportURL: '/sys/user/export'
      },
      dataForm: {

        productTypeId: '',
        taskName: ''
      }
    }
  },
  created () {
    this.loadProductType()
  },
  mounted () {
  },
  methods: {
    loadProductType () {
      this.$http
        .get('/product/pproductinfo/producttypelist', {
          params: { page: 1, limit: 100 }
        })
        .then(res => {
          // console.log(res)
          this.producttypes = res.data.data
        })
    },
    handleResponseListData (data) {
      // console.log(data)
      if (!this.producttypes || this.producttypes.length <= 0) {
        return data
      }
      data = data.map(item => {
        let pId = item.productTypeId
        let type = this.producttypes.find(item => {
          return item.id === pId
        })
        item.productTypeName = type.productType
        item.productTypeCode = type.typeCode
        return item
      })
      return data
    },

    getProductTypeName (item) {
      let pId = item.productTypeId
      let type = this.producttypes.find(item => {
        return item.id === pId
      })

      return `${type.productType}(${type.typeCode})`
      // item.productTypeName = type.productType
      // item.productTypeCode = type.typeCode
    },

    onTypeChanged (value) {
      // console.log(value)
      this.dataForm.productTypeId = value
      this.getDataList()
    },
    sn8Changed (sn8) {
      this.dataForm.sn8 = sn8
    },
    clickOnAddOrUpdate (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        console.log(this.$refs.addOrUpdate)
        this.$refs.addOrUpdate.form.id = id
        // this.$refs.addOrUpdate.dataForm.sn8 = this.dataForm.sn8
        this.$refs.addOrUpdate.init()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.production-test-case-page {
  .task-content{
    max-height: 200px;
    min-height: 80px;
    overflow-y: auto;
    padding: 10px;
    background-color: #f9f9f9;
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}
</style>
